﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

/*
 <?xml version="1.0" encoding="utf-8"?>
<Appdomain>
  <AppInfo CustomAppName="QQ" EXEAppName="QQ.exe" IconName="QQ" GroupName="Common">C:\Program Files (x86)\Git\git-bash.exe</AppInfo>
  <AppInfo CustomAppName="XX" EXEAppName="XX.exe" IconName="XX" GroupName="Specific">C:\Program Files (x86)\Git\fdfd.exe</AppInfo>
</Appdomain>
 */

namespace WindowsFormsForControlTest
{
    public class AppInfo
    {
        public AppInfo(string customAppName, 
            string exeAppName,
            string iconName,
            string fullPath,
            string groupName,
            string Tips)
        {
            this.CustomAppName = customAppName;
            this.EXEAppName = exeAppName;
            this.IconName = iconName;
            this.FullPath = fullPath;
            this.GroupName = groupName;
            this.Tips = Tips;

        }

        public string CustomAppName;
        public string EXEAppName;
        public string IconName;
        public string FullPath;
        public string GroupName;
        public string Tips;
    }

    class XmlHelper
    {
        private static XmlHelper instane;
        public static XmlHelper _Instance
        {
            get {
                if (instane == null)
                {
                    instane = new XmlHelper();
                    return instane;
                }
                return instane;
            }
        }

        public List<AppInfo> AllAppPath = new List<AppInfo>();

        private XmlDocument xmlDoc = new XmlDocument();
        private XmlNode rootNode;

        public XmlHelper()
        {
            if (!File.Exists(Constant.XMLPath)) { MessageBox.Show("没有找到xml！"); return; }

            try
            {
                xmlDoc.Load(Constant.XMLPath);
                XmlLoad();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return;
            }
        }

        #region Dynamic
        public void XmlLoad()
        {
            rootNode = xmlDoc.SelectSingleNode("Appdomain");
            XmlNodeList nodeList = rootNode.ChildNodes;

            if (nodeList.Count <= 0) return;

            foreach (XmlNode node in nodeList)
            {
                XmlElement e = node as XmlElement;
                string cusAppName = e.GetAttribute("CustomAppName");
                string exeAppName = e.GetAttribute("EXEAppName");
                string iconName = e.GetAttribute("IconName");
                string goupName = e.GetAttribute("GroupName");
                string fullpath = node.InnerText;
                string tips = e.GetAttribute("MyTips");
                AppInfo appInfo = new AppInfo(cusAppName, exeAppName, iconName, fullpath, goupName, tips);

                if (AllAppPath == null || AllAppPath.Contains(appInfo)) break;

                AllAppPath.Add(appInfo);
            }

            return;
        }
        #endregion
    }
}
